package com.sxfq.loansupermarket.backsystem.modular.marketing.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.common.persistence.model.Deptinfos;
import com.sxfq.loansupermarket.backsystem.common.persistence.model.ExtenAccountInfo;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.core.util.ToolUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.basicData.service.IOutpatientManagementService;
import com.sxfq.loansupermarket.backsystem.modular.marketing.entity.*;
import com.sxfq.loansupermarket.backsystem.modular.marketing.service.ICallRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.*;


/**
 * @author py
 * @Date
 */
@Controller
@RequestMapping("/reservationsCallRecord")
public class ReservationsCallRecordController extends BaseController {
    @Autowired
    ICallRecordsService callRecordsService;

    @Autowired
    IOutpatientManagementService outpatientManagementService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SqlMapper sqlMapper;

    private String PREFIX = "/marketing/reservations/";

    /**
     * 跳转到市场通话记录
     */
    @RequestMapping("")
    public String reservationsCallRecord(Model model) {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "Abbreviation", 0) + "";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        if (redisTemplate.opsForList().index("arealist", 0) == null) {
            List<ExtenAccountInfo> arealist = callRecordsService.getArealist();
            redisTemplate.opsForList().leftPush("arealist", arealist);
        }
        model.addAttribute("areaList", redisTemplate.opsForList().index("areaList",0));
        model.addAttribute("userName", ShiroKit.getUser().getAccount());
        return PREFIX + "callRecord.html";
    }

    /**
     * 删除回访
     */
    @RequestMapping(value = "/handleMissed")
    @ResponseBody
    public int handleMissed(@Valid ReservationsCallInfo reservationsCallInfo) {
        return callRecordsService.handleMissed(reservationsCallInfo);
    }

    @RequestMapping("/to_handleMissed/{flowNo}/{customerPhone}/{id}")
    public String to_handleMissed(@PathVariable String flowNo,@PathVariable String customerPhone,@PathVariable String id, Model model) {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "Abbreviation", 0) + "";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        model.addAttribute("flowNo", flowNo);
        model.addAttribute("customerPhone", customerPhone);
        model.addAttribute("id", id);
        return PREFIX + "handleMissed.html";
    }

    /**
     * 跳转到通话记录统计
     */
    @RequestMapping("/total")
    public String reservationsCallRecordTotal(Model model) {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "Abbreviation", 0) + "";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        List<Map<String, Object>> outpatientManagementList = outpatientManagementService.lists(ShiroKit.getUser().getAccount());
        model.addAttribute("areaList", redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"areaList",0));
        model.addAttribute("areaAbbreviationValue", ShiroKit.getUser().getAreaAbbreviation());
        model.addAttribute("userName", ShiroKit.getUser().getAccount());
        model.addAttribute("areaValues", ShiroKit.getUser().getAccount());
        return PREFIX + "callRecordTotal.html";
    }

    /**
     * 个人通话记录列表
     */
    @RequestMapping("/callRecordlist/{customerPhone}")
    @ResponseBody
    public List<ReservationsCallInfo> recondList(@PathVariable String customerPhone) {
        List<ReservationsCallInfo> list = callRecordsService.selectGeCallRecond(customerPhone);
        return list;
    }

    /**
     * 个人通话记录列表
     */
    @RequestMapping("/returnVlist/{customerPhone}")
    @ResponseBody
    public List<ReturnVInfo> returnVlist(@PathVariable String customerPhone) {
        List<ReturnVInfo> list = callRecordsService.returnVlist(customerPhone);
        return list;
    }

    /**
     * 通话记录列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object listYD(ReservationsCallInfo info, HttpServletRequest request) {
        Page<ReservationsCallInfo> page = new PageFactory<ReservationsCallInfo>().defaultPage();
        try {
            info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
            info.setLimitIndex(page.getSize());
            page.setCurrent(0);
            if ("1".equals(info.getCallStatus())) {
                List<ReservationsCallInfo> list = callRecordsService.selectSecondByCallRecond(page, info);
                if (list.size() == 0) {
                    info.setStartIndex(0);
                    list = callRecordsService.selectSecondByCallRecond(page, info);
                }
                page.setRecords(list);
                page.setTotal(callRecordsService.getQueryPagesCount(page, info));
            }
            if ("2".equals(info.getCallStatus())) {
                List<ReservationsCallInfo> list = callRecordsService.selectSecondByCallInRecond(page, info);
                if (list.size() == 0) {
                    info.setStartIndex(0);
                    list = callRecordsService.selectSecondByCallInRecond(page, info);
                }
                page.setRecords(list);
                page.setTotal(callRecordsService.getQueryPagesInCount(page, info));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    /**
     * 通话记录统计
     */
    @RequestMapping("/callTotalList")
    @ResponseBody
    public Object listTotalYD(ReservationsCallInfo info) {
        Page<ReservationsCallInfo> page = new PageFactory<ReservationsCallInfo>().defaultPage();
        if (info.getAreaAbbreviation() == "" || info.getAreaAbbreviation() == null || info.getAreaAbbreviation() == "null") {
            return null;
        } else {
            try {
                List<ReservationsCallInfo> list = callRecordsService.selectCallRecondTotal(info);
                page.setRecords(list);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return super.packForBT(page);
        }
    }

    /**
     * 未到点击姓名跳转客户管理详情页面
     */
    @RequestMapping(value = "/checkRecond/{id}/{phone}")
    public String checkRecond(@PathVariable String id,@PathVariable String phone, Model model) {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "Abbreviation", 0) + "";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        if (ToolUtil.isEmpty(phone)) {
            return "/errorClose.html";
        }
        try {
            model.addAttribute("id", id);
            model.addAttribute("phone", phone);
            return PREFIX + "checkRecond.html";
        } catch (Exception e) {
            return "/errorClose.html";
        }
    }

    /**
     *
     */
    @RequestMapping(value = "/getWanName", method = RequestMethod.POST)
    @ResponseBody
    public List<ExtenAccountInfo> getWanName(String area) {
        List<ExtenAccountInfo> getWanNameList = callRecordsService.getWanNameList(area);
        return getWanNameList;
    }
}